﻿//------------------------------------------------------------------------------ 
// <copyright file="JWaterMark.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JWaterMark.watermark.css", "text/css")]

namespace FmjQuery
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// jQuery TextBox
    /// </summary>
    [ToolboxData("<{0}:JWaterMark runat=server />")]
    [DefaultProperty("TargetControlID")]
    [NonVisualControl]
    public class JWaterMark : JQueryExtendControl
    {
        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JWaterMark class.
        /// </summary>
        public JWaterMark()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property WaterMark
        /// <summary>
        /// Gets or sets Text for WaterMark
        /// </summary>
        [Description("Text for WaterMark, if not insert value use Text Property")]
        public string WaterMarkText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Css Class for WaterMark
        /// </summary>
        [Description("Css Class for waterMark")]
        public string WaterMarkCss
        {
            get;
            set;
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Gets resources
        /// </summary>
        /// <returns>collection of resource</returns>
        public override JWebSources GetResources()
        {
            return string.IsNullOrEmpty(this.WaterMarkCss) ? new JWebSources { new JSource(Properties.Settings.Default.Css_WaterMark_Name, Properties.Settings.Default.Css_WaterMark_Url, JSourceType.css, true) } : null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            Manager.FrameWorkExtend = true;

            if (this.Enabled)
            {
                // check param
                TextBox ctrl = (TextBox) FindControl(this.TargetControlID);
                string water = this.WaterMarkText ?? ctrl.Text;

                if (string.IsNullOrEmpty(water))
                {
                    throw new InvalidOperationException("You must insert Text or WaterText");
                }

                Utility.RegisterStartUpScript(Page, string.Concat("water_", this.ClientID), string.Format("$('#{0}').createWaterMark('{1}','{2}');", ctrl.ClientID, water, this.WaterMarkCss ?? "waterMark_jquery"));
            }
        }
        #endregion
    }
}
